<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentAddAlbum.class.php, $Author:  $ */



/** Component for adding new album
 *   
 * @version $Id: PhpFox_ComponentAddAlbum.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentAddAlbum extends BaseComponent
{
    function process()
    {
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oTpl = &$this->_oModule->getTemplate();
        App::loadClass('Validator');

        $oSecurityService->checkPerm('music', array('full'));
        $oSecurityService->checkAccess('music');
        $aFields = array
        (
            'val[musician_name]'     => array('title' => App::format('musician_name'), 'def'    => 'required'),
            'val[album_title]'       => array('title' => App::format('album_title'),   'def'    => 'required'),
            'val[album_year]'        => array('title' => App::format('year'),    'min'    => 1901,       'max' => date('Y'), 'optional' => true),
            'val[album_description]' => array('title' => App::format('description'),   'maxlen' => 3000,       'optional' => true),
        );
        $oValidator = &new Validator($aFields);
        if ($oReq->getAction() == 'create')
        {
            $aValues = $oReq->getArray('val');
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            if (!count($this->getErrors()))
            {
                $oCurrentUser = $oSecurityService->getCurrentUser();
                
                $aAlbumData = array
                (
                    'album_title'       => htmlspecialchars($aValues['album_title']),
                    'musician_name'     => htmlspecialchars($aValues['musician_name']),
                    'album_year'        => ($aValues['album_year']) ? $aValues['album_year'] : '0000',
                    'genre_id'          => $aValues['genre_id'],
                    'album_description' => htmlspecialchars($aValues['album_description']),
                );
                
                $iAlbumId = $oMusicService->addAlbum($aAlbumData, $oCurrentUser->aData['id']);
                if (!$iAlbumId)
                {
                    $this->addErrors($oMusicService->getErrors());
                }
            }
            if (!count($this->getErrors()))
            {
                App::gotoUrl('public.account.submit.music.album-song', array('id' => $iAlbumId), App::format('Mod_Music.info.album_created'), false, 'music_upload');
            }
            $oTpl->assign(array(
                'aErrors' => $this->getErrors(),
                'aValue'  => $aValues,
            ));
        }

        $oTpl->assignSrc(array(
            'sValidatorJs'   => $oValidator->makeJS(),
            'sValidatorMess' => $oValidator->makeJsMess(),
        ));
        $oTpl->assign(array(
            'aGenres'      => $oMusicService->getGenres(),
            'iMaxSongSize' => App::getParam('Mod_Music.max_song_size'),
        ));
        return $oTpl->fetch('CreateAlbum.html');
    }
}
?>